package com.andy.blog.dal.service.impl;

import com.andy.blog.common.enums.UserStatusEnum;
import com.andy.blog.common.exception.BlogServiceException;
import com.andy.blog.common.reponse.BlogResultCode;
import com.andy.blog.common.utils.SecurityUtils;
import com.andy.blog.common.utils.TransformUtils;
import com.andy.blog.dal.domain.TagDO;
import com.andy.blog.dal.mapper.TagMapper;
import com.andy.blog.dal.service.TagDalService;
import com.andy.blog.dal.vo.TagVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * Created by andy on 2023/6/17 14:15
 */
@Service
public class TagDalServiceImpl implements TagDalService {

    @Resource
    private TagMapper tagMapper;

    /**
     * 构建查询条件
     */
    private LambdaQueryWrapper<TagDO> buildQueryWrapper(TagVO tagVO) {
        return new LambdaQueryWrapper<TagDO>()
                .like(StringUtils.isNotBlank(tagVO.getTagName()), TagDO::getTagName, tagVO.getTagName())
                .eq(StringUtils.isNotBlank(tagVO.getStatus()), TagDO::getStatus, tagVO.getStatus());
    }

    @Override
    public IPage<TagDO> selectListByPage(TagVO tagVO) {
        LambdaQueryWrapper<TagDO> lambdaQueryWrapper = buildQueryWrapper(tagVO);
        IPage<TagDO> page = new Page<>(tagVO.getPageNum(), tagVO.getPageSize());
        return tagMapper.selectPage(page, lambdaQueryWrapper);
    }

    @Override
    public void addTag(TagVO tagVO) {
        TagDO tagDO = TransformUtils.transform(tagVO, TagDO.class);
        if (Objects.isNull(tagDO)) {
            throw new BlogServiceException(BlogResultCode.BLOG_000001, "入参不能为空");
        }

        tagDO.setCreateBy(SecurityUtils.getUsername());

        int row;
        try {
            row = tagMapper.insert(tagDO);
        } catch (Exception e) {
            throw new BlogServiceException(BlogResultCode.BLOG_000100, e);
        }

        if (row != 1) {
            throw new BlogServiceException(BlogResultCode.BLOG_000100);
        }
    }

    @Override
    public TagDO selectById(Long tagId) {
        return tagMapper.selectById(tagId);
    }

    @Override
    public void updateById(TagVO tagVO) {
        TagDO tagDO = TransformUtils.transform(tagVO, TagDO.class);
        if (Objects.isNull(tagDO)) {
            throw new BlogServiceException(BlogResultCode.BLOG_000001, "入参不能为空");
        }

        tagDO.setUpdateBy(SecurityUtils.getUsername());

        int row;
        try {
            row = tagMapper.updateById(tagDO);
        } catch (Exception e) {
            throw new BlogServiceException(BlogResultCode.BLOG_000101, e);
        }

        if (row != 1) {
            throw new BlogServiceException(BlogResultCode.BLOG_000100);
        }
    }

    @Override
    public List<TagDO> selectList(TagVO tagVO) {
        tagVO.setStatus(UserStatusEnum.OK.getCode());
        LambdaQueryWrapper<TagDO> lambdaQueryWrapper = buildQueryWrapper(tagVO);
        return tagMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public List<TagDO> selectBatchIds(List<Long> ids) {
        return tagMapper.selectBatchIds(ids);
    }

    @Override
    public Long selectTagCount(TagVO tagVO) {
        LambdaQueryWrapper<TagDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        return tagMapper.selectCount(lambdaQueryWrapper);
    }
}
